package com.wang.pojo;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wang.common.pojo.BasePojo;
import com.wang.common.util.DateFormatUtil;

/**
 * email的pojo
 * 
 * @author MyJjh
 *
 */
public class AEmail extends BasePojo<AEmail> {
	private int id;
	private String tag;
	private String rcpt;
	private String sender;
	private String region;
	private String ip;
	private String fromname;
	private String url ; 
	private String content;
	private String authuser ; 
	private byte status;
	private Date createTime;
	private Date updateTime;
	private Date pubTime;
	private Date sendTime;

	/* 字符串描述 */
	private String statusStr;
	private DateFormatUtil dateFormatUtil = new DateFormatUtil();

	/* 枚举值 */
	private Map<String, Object> enumsMap = new TreeMap<String, Object>();

	public AEmail parse(String jsonStr) {
		JSONObject emailJSON = (JSONObject) JSON.parse(jsonStr);
		this.setTag(emailJSON.getString("tag"));
		this.setRcpt(emailJSON.getString("rcpt"));
		this.setSender(emailJSON.getString("sender"));
		this.setRegion(emailJSON.getString("region"));
		this.setIp(emailJSON.getString("ip"));
		this.setFromname(emailJSON.getString("fromname"));
		this.setContent(emailJSON.getString("content"));
		this.setUrl(emailJSON.getString("url"));
		String sendTime = emailJSON.getString("@timestamp") ;
		if(sendTime != null) {
			sendTime = sendTime.replaceAll("T", " ").replaceAll("Z", "") ; 
			this.setSendTime(dateFormatUtil.strDate(sendTime,"yyyy-MM-dd hh:mm:ss"));
		}else
		{
			this.setSendTime(new Date());
		}
		this.setCreateTime(new Date());
		this.setUpdateTime(new Date());
		this.setPubTime(new Date());
		return this ; 
	}

	/**
	 * 获取一个map; map有两个要素：键和值,而枚举有三个要素:code,info,枚举项
	 * 
	 * @return map的键是:枚举项-code值,值是:info
	 */
	public Map<String, Object> getEnumsMap() {
		for (AEmailEnum enumTemp : AEmailEnum.values()) {
			enumsMap.put(enumTemp.toString() + "-" + enumTemp.getCode(), enumTemp.getInfo());
		}
		return enumsMap;
	}

	/**
	 * 根据status的值,和枚举中对应的值, 返回枚举对应的字符串
	 * 
	 * @return
	 */
	public String getStatusStr() {
		for (AEmailEnum enumTemp : AEmailEnum.values()) {
			if (enumTemp.toString().startsWith("STATUS_") && enumTemp.getCode() == this.getStatus()) {
				return enumTemp.getInfo();
			}
		}
		return statusStr;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getRcpt() {
		return rcpt;
	}

	public void setRcpt(String rcpt) {
		this.rcpt = rcpt;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getFromname() {
		return fromname;
	}

	public void setFromname(String fromname) {
		this.fromname = fromname;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public byte getStatus() {
		return status;
	}

	public void setStatus(byte status) {
		this.status = status;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Date getPubTime() {
		return pubTime;
	}

	public void setPubTime(Date pubTime) {
		this.pubTime = pubTime;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getAuthuser() {
		return authuser;
	}

	public void setAuthuser(String authuser) {
		this.authuser = authuser;
	}

	public Date getSendTime() {
		return sendTime;
	}

	public void setSendTime(Date sendTime) {
		this.sendTime = sendTime;
	}

}
